//注册功能已经完成捕获异常改写


package auto.common;


import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Registartion {

    public static String  registartion(WebDriverWait wait, AppiumDriver driver, String accountUser, String passwordUser) throws InterruptedException {
        try{
            //如果没有历史账号则会使用外测服务器进入
            Thread.sleep(500);
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"进入\"`]")).size()>=1){
                driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"进入\"`]")).click();
            }else{
                System.out.println("非首次进入服务器");
            }

            //判断历史页面是否存在，历史页面如果存在则需要点击添加账号才能进入输入页面
            if(driver.findElements(AppiumBy.accessibilityId("添加其他账户")).size()>=1){
                driver.findElement(AppiumBy.accessibilityId("添加其他账户")).click();
                //点击添加之后，判断是否达到上限
                if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"账户添加上限\"`]")).size()>=1){
                    System.out.println("历史账号页面已经达到上限，清理后再执行注册");
                    return "历史账号页面已经达到上限，清理后再执行注册";
                }
            }else{
                System.out.println("不存在历史账号页面");
            }


            //在这里判断是邮箱还是手机号，如果含有就不用执行任何操作，如果不包含就要进行切换
            if (accountUser.contains("@")) {
                WebElement textField1 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                        ("**/XCUIElementTypeTextField[`value == \"请输入你的邮箱\"`]")));
                textField1.click();
                textField1.sendKeys(accountUser);
            } else {
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("手机号码登录"))).click();
                //点击输入号码的文本框
                WebElement textField2 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                        ("**/XCUIElementTypeTextField[`value == \"输入电话号码\"`]")));
                textField2.click();
                textField2.sendKeys(accountUser);
            }

            //输入号码之后点击获取验证码
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeButton[`name == \"获取验证码\"`][2]"))).click();

            //处理异常情况
            if (driver.findElements(AppiumBy.accessibilityId("确定")).size() >= 1) {
                System.out.println("该账号已经注册过");
                return "该账号已经注册过";
            }
            if (driver.findElements(AppiumBy.accessibilityId("该账号已登录")).size() >= 1) {
                System.out.println("该账号已登录");
                return "该账号已登录";
            } else if (driver.findElements(AppiumBy.accessibilityId("电话号码无效")).size() >= 1) {
                System.out.println("输入账号是无效的");
                return "输入账号无效";
            } else if (driver.findElements(AppiumBy.accessibilityId("邮箱格式错误")).size() >= 1) {
                System.out.println("输入邮箱格式错误");
                return "邮箱格式错误";
            }

            System.out.println("10s内滑块验证手动实现");

            //外测中使用的固定的验证码
            String verify = "306908";
            WebElement verificationCode =wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextField")));
            verificationCode.sendKeys(verify);


            //输入昵称
            WebElement nickName = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeTextField[`value == \"请输入您的昵称\"`]")));
            nickName.click();
            nickName.sendKeys(accountUser);


            //点击下一步
            WebElement next = wait.until(ExpectedConditions.visibilityOfElementLocated
                    (AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"下一步\"`]")));
            wait.until(ExpectedConditions.attributeToBe(next, "enabled", "true"));//灰色的是false，变蓝了才能点击
            next.click();

            //第一次输入两步验证
            WebElement pinCode = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeSecureTextField[`value == \"请输入两步验证密码\"`]")));
            pinCode.click();
            pinCode.sendKeys(passwordUser);


            //等待下一步按钮高亮开始点击
            WebElement next1 = wait.until(ExpectedConditions.visibilityOfElementLocated
                    (AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"下一步\"`]")));
            wait.until(ExpectedConditions.attributeToBe(next1, "enabled", "true"));//灰色的是false，变蓝了才能点击
            next1.click();

            //第二次输入两步验证
            WebElement pinCode1 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeSecureTextField[`value == \"请输入两步验证密码\"`]")));
            pinCode1.click();
            pinCode1.sendKeys(passwordUser);


            //两步验证输入完之后点击下一步。需要等元素的enabled元素可见
            WebElement next2 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == '下一步'`]")));
            wait.until(ExpectedConditions.attributeToBe(next2, "enabled", "true"));//灰色的是false，变蓝了才能点击
            next2.click();

            //开始输入两步验证密码的提示
            WebElement passwordTip=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"密码提示（可选）\"`]")));
            passwordTip.sendKeys("123456");

            //点击出现的下一步
            WebElement next3 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == '下一步'`]")));
            wait.until(ExpectedConditions.attributeToBe(next3, "enabled", "true"));//灰色的是false，变蓝了才能点击
            next3.click();

            //输入恢复邮箱
            WebElement restoreEmail=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"输入您的邮箱地址\"`]")));
            restoreEmail.sendKeys("123456@gmail.com");

            //点击出现的下一步
            WebElement next4 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == '下一步'`]")));
            wait.until(ExpectedConditions.attributeToBe(next4, "enabled", "true"));//灰色的是false，变蓝了才能点击
            next4.click();

            //需要进行滑块验证，10秒内完成
            System.out.println("请在10秒内完成滑块验证");

            //输入恢复邮箱收到的验证码，外测环境依然是306908
            String verify1 = "306908";
            WebElement verificationCode1 =wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextField")));

            verificationCode1.sendKeys(verify1);

            //点击进入zebra的按钮
            WebElement enterApp = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == '进入Zebra Messenger'`]")));
            wait.until(ExpectedConditions.attributeToBe(enterApp, "enabled", "true"));//灰色的是false，变蓝了才能点击
            enterApp.click();

            //查看是否进入聊天列表页面
            try{
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("AiAssist")));
            }catch(TimeoutException e){
                System.out.println("没有成功进入聊天列表页面");
                return "没有成功进入聊天列表页面";
            }

            System.out.println("注册功能正向流程正常");
            return "正常";
        }catch(TimeoutException | NoSuchElementException e1){
            System.out.println("注册流程出现异常，请检查");
            return "注册流程出现异常，请检查";
        }
    }
}
